;This file contains header and footer of different file formats, executed conditionally by a script to produce output
warnings disable W1008
if !output_format == !INES_FORMAT ; If so, add iNES header.  These symbols are defined by command line parameters calling the assembler.
print "Assembling NES binary..."
db "NES", $1a ; iNES header magic value
db $02 ; Uses two 16 KB PRG ROM
db $00; Uses one 8 KB CHR ROM
db $01, $00 ;  Uses mapper 0, and vertical mirroring
fill $08
elseif !output_format == !FDS_FORMAT ; If so, add FDS header.  These symbols are defined by command line parameters calling the assembler.
print "Assembling FDS binary..."
!skip_license_screen = 0
db "FDS", $1a ; FDS header magic value
db $01 ; Number of sides
fill $0b ; Reserved

db $01 ; Block type, denotes block 1
db "*NINTENDO-HVC*" ; Disk ID
db $00 ; Manufacturer ID
db "TEST" ; Game name
db $00 ; Revision of program
db $00 ; Side number, 0 indicates a bootable disk
db $00 ; Disk number, 0 indicates a bootable disk
db $00, $00 ; Extra disk ID field
db $05+!skip_license_screen ; Highest file ID to load up to at boot
fill $1e ; Reserved

db $02 ; Denotes block 2
db $05+!skip_license_screen ; File count

; File 0 header
db $03 ; Denotes block 3
db $00 ; File number
db $00 ; File ID
db "PROGRAM0" ; File name (unused)
dw code_entry_point
dw code_entry_point_end-code_entry_point
db $00 ; Target RAM area

; File 0 data
db $04 ; Denotes block 4
base $6000
code_entry_point:
if !skip_license_screen
code_to_skip_the_license_screen:
lda #$00
sta $2000

lda.b #(nmi&$ff)
sta $dffa
lda.b #(nmi>>$08)
sta $dffb

lda #$35
sta $0102
lda #$ac
sta $0103

jmp ($fffc)
endif
elseif !output_format == !NSF_FORMAT ; If so, add NSF header.  These symbols are defined by command line parameters calling the assembler.
print "Assembling NSF binary..."
db "NESM", $1a ; NSF header magic value
db $01 ; NSF specification version number
db $04 ; Number of tunes
db $00 ; Index of the starting tune
dw program ; Address to load data from
dw sound_init ; Address to init data from
dw sound_play ; Address to play audio-related data from
db "RP2A03 chip audio driver test" : - : fill $2e-((-)&$ff) ; The name of the tune
db "<?>" : - : fill $4e-((-)&$ff) ; The name of its artist
db "<?>" : - : fill $6e-((-)&$ff) ; The name of its copyright holder
dw $411a ; Playback rate on NTSC machines
db "00000000" ; Determines where each region of memory in FDS software is to be bankswitched
dw $4e1d ; Playback rate on PAL machines
warnings enable 1008
db %00000000 ; Bit 0 determines whether to play this tune as NTSC or PAL, when clear or set, respectively;  bit 1 determines whether to play this tune as a dual NTSC  and PAL one, when set, instead.
db %00000000 ; Bits from 0 to 5 determine support for the VRC6, VRC7, FDS, MMC5, Namco 163, and, or, Sunsoft 5B extra sound chip or chips is enabled, in this order, respectively.
db $00 ; This is reserved for the NSF2 specification
dl $000000 ; The length of the program data;  a value of 0 is interpreted as that all data until the end of this file is part of the program.
endif ; End of header section
warnings enable W1008

base $8000
incsrc "bss.s" ; Uninitialized variables, memory map
program:

incsrc "data.s" ; Generated data, binary assets and tables

if !output_format == !INES_FORMAT || !FDS_FORMAT
reset:
sei ; Turns off IRQs
cld ; Turns off decimal mode

ldx #$40
stx $4017 ; disable APU frame IRQ
ldx #$FF
txs ; Set up stack
inx ; now X = 0
stx $2000 ; disable NMI
stx $2001 ; disable rendering
stx $4010 ; disable DMC IRQs

; Clear memory
jsr wait_for_vblank ; First wait for vblank to make sure PPU is ready
-:--
txa
sta $0000, x
sta $0100, x
sta $0200, x
sta $0400, x
sta $0500, x
sta $0600, x
sta $0700, x
lda #$FE
sta $0300, x
inx
bne -
jsr wait_for_vblank ; Second wait for vblank, PPU is ready after this
lda #$00 ; Audio track number to load
jsr sound_init

lda #%10000000   ; enable NMI, sprites from Pattern Table 0
sta $2000

lda #%00011000
sta $2001


-: ; Main loop
clv
bvc -
wait_for_vblank:
bit $2002
bpl wait_for_vblank
rts
nmi:
jsr sound_play
irq:
rti
endif ; End of iNES build conditional compilation

incsrc "soundeng.s"

if !output_format == !INES_FORMAT
vectors:
check bankcross off
fill 2**16-($ffff&pctosnes(vectors))-$06
dw nmi
dw reset
dw irq

elseif !output_format == !FDS_FORMAT
code_entry_point_end:

; File 1 header
db $03 ; Denotes block 5
db $01 ; File number
db $01 ; File ID
db "VECTORS." ; File name (unused)
dw $dff6
dw $000a
db $00 ; Target RAM area

; File 1 data
db $04 ; Denotes block 6
dw nmi
dw nmi
if !skip_license_screen
dw code_to_skip_the_license_screen
else
dw nmi ; Or could be used to bypass the license screen
endif
dw reset
dw irq

; File 2 header
db $03 ; Denotes block 7
db $02 ; File number
db $02 ; File ID
db "BKGTILES" ; File name (unused)
dw $0000
dw $1000
db $01 ; Target RAM area

; File 2 data
db $04 ; Denotes block 8
incbin backgrnd.chr

; File 2 header
db $03 ; Denotes block 9
db $03 ; File number
db $03 ; File ID
db "SPRTILES" ; File name (unused)
dw $1000
dw $1000
db $01 ; Target RAM area

; File 3 data
db $04 ; Denotes block 10
incbin sprites.chr

; File 4 header
db $03 ; Denotes block 11
db $04 ; File number
db $04 ; File ID
if !skip_license_screen
db "PROGRAM1" ; File name (unused)
dw $2000
dw $0001
db $00 ; Target RAM area
; File 4 data
db $04 ; Denotes block 12
db %10010000 ; Enables NMI byte sent to $2000

else
db "KYODAKU-" ; File name (Magic data)
dw $2800
dw $00e0
db $02 ; Target RAM area
; File 4 data
db $04 ; Denotes block 12
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $17, $12, $17, $1D, $0E
db $17, $0D, $18, $24, $28, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $0F
db $0A, $16, $12, $15, $22, $24, $0C, $18
db $16, $19, $1E, $1D, $0E, $1B, $24, $1D
db $16, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $24, $24, $24, $24, $24, $24
db $24, $24, $1D, $11, $12, $1C, $24, $19
db $1B, $18, $0D, $1E, $0C, $1D, $24, $12
db $1C, $24, $16, $0A, $17, $1E, $0F, $0A
db $0C, $1D, $1E, $1B, $0E, $0D, $24, $24
db $24, $24, $0A, $17, $0D, $24, $1C, $18
db $15, $0D, $24, $0B, $22, $24, $17, $12
db $17, $1D, $0E, $17, $0D, $18, $24, $0C
db $18, $27, $15, $1D, $0D, $26, $24, $24
db $24, $24, $18, $1B, $24, $0B, $22, $24
db $18, $1D, $11, $0E, $1B, $24, $0C, $18
db $16, $19, $0A, $17, $22, $24, $1E, $17
db $0D, $0E, $1B, $24, $24, $24, $24, $24
db $24, $24, $15, $12, $0C, $0E, $17, $1C
db $0E, $24, $18, $0F, $24, $17, $12, $17
db $1D, $0E, $17, $0D, $18, $24, $0C, $18
db $27, $15, $1D, $0D, $26, $26, $24, $24
endif

-:
check bankcross off
fill (2**15)+65516-($ffff&pctosnes(-))


endif ; End of format conditional compilation
